
import {Injectable} from '@angular/core';
import {DataStorageUtil} from './data-storage-util';
import {TokenUtil} from './token-util';
import {AppConfig} from '../app.config';

declare var $: any;

@Injectable()
export class TableUtil {
  private toggleSelector = '.sidebar-toggle';
  private table: any;

  constructor(
    private dataStorage: DataStorageUtil,
    private tokenUtil: TokenUtil
  ) {}
  /**
   * 获取所有rows中某字段值
   * @param rows
   * @param cloumnName
   * @returns {any[]}
   */
  getCloumns(rows: Array<Object>, cloumnName: string): Array<string> {
    return rows.map(function(row) {
      if (!!row[cloumnName]) {
        return row[cloumnName];
      }
    })
  }

  getCloumn(row: Object, cloumnName: string): string {
    return row[cloumnName];
  }
  /**
   * 获取id数组
   * @param rows
   * @returns {Array<string>}
   */
  getIds(rows: Array<Object>): Array<string> {
    return this.getCloumns(rows, 'id');
  }

  /**
   * 获取id
   * @param row
   * @returns {string}
   */
  getId(row: Object): string {
    return this.getCloumn(row, 'id');
  }

  /**
   * 封装bootstrap table初始加载表
   * @param id
   * @param settings
   * @returns {any}
   */
  load(id: any, settings: any) {
    const table = $('#' + id).bootstrapTable($.extend({
      ajaxOptions: {
        headers: (AppConfig.authType && 'jwt' ===  AppConfig.authType.toLowerCase()) ?
          {'Authorization': 'Bearer ' + this.tokenUtil.getToken()} : null
      }
    }, settings));
    this.bindResetTableView(table);
    if (settings.hasOwnProperty('pagination') &&　settings.pagination) {
      this.preventPaginationDefault();
    }
    return table;
  }

  /**
   * 通过sidebar-toggle的点击重置表格的视图
   * @param table
   */
  bindResetTableView = (table: any) => {
    this.table = table;
    $(this.toggleSelector).unbind('click', this.ResetTableView);
    $(this.toggleSelector).click(this.ResetTableView);
  };
  ResetTableView = () => {
    setTimeout(() => {
      this.table.bootstrapTable('resetView');
    }, 300)
  };

  /**
   * 阻止表格分页的默认事件
   */
  preventPaginationDefault() {
    $('.one-table-area').on('click', '.pagination a' , (e) => {
      e.preventDefault();
    });
  }

  /**
   * 显示省略内容
   * @param value
   * @param row
   * @param index
   * @returns {string}
   */
  showToolTip = (value: any, row: any, index: any) => {
    return "<p data-toggle='tooltip' title='" + value + "' class='text-ellipsis "  +
      "' data-placement='bottom'>" + value + "</p>"
  }

  /**
   * 显示内容并换行
   * @param value
   * @param row
   * @param index
   * @returns {string}
   */
  showToolTipChangeLine = (value: any, row: any, index: any) => {
    return "<p data-toggle='tooltip' style='width: 300px;  white-space: normal' title='" + value + "' class='text-ellipsis "  +
      "' data-placement='bottom'>" + value + "</p>"
  };
  /**
   * 刷新表格参数
   * @param {object} options  需要刷新的配置
   */
  refreshOptions = (options?: object) => {
    let tableOptions = {
      ajaxOptions: {
        headers: (AppConfig.authType && 'jwt' ===  AppConfig.authType.toLowerCase()) ?
          {'Authorization': 'Bearer ' + this.tokenUtil.getToken()} : null
      }
    };
    if (options && !(options instanceof Array) && (options instanceof Object)) {
      tableOptions = Object.assign(tableOptions, options);
    }
    this.table.bootstrapTable('refreshOptions', tableOptions);
  };
}
